package com.example.test;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationSet;
import android.widget.Button;
import android.widget.ImageView;

public class ObjectActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView iv_ic;
    private Button trans,rototas,alps,scales,set;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_object);
        iv_ic= (ImageView) findViewById(R.id.iv_ic);
        trans= (Button) findViewById(R.id.trans);
        rototas= (Button) findViewById(R.id.rototas);
        alps= (Button) findViewById(R.id.alps);
        scales= (Button) findViewById(R.id.scales);
        set= (Button) findViewById(R.id.set);
        trans.setOnClickListener(this);
        rototas.setOnClickListener(this);
        alps.setOnClickListener(this);
        scales.setOnClickListener(this);
        set.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.trans:
                transAnim();
            break;
            case R.id.rototas:
                rototasAnim();
                break;
            case R.id.alps:
                alpsAnim();
                break;
            case R.id.scales:
                scalesAnim();
                break;
            case R.id.set:
                setAnim();
                break;
            }
    }

    /**
     * 综合动画,属性动画可以叠加，也就是说，可以综合写，也可以点击不同按钮，效果会叠加。
     */
    private void setAnim() {
        AnimatorSet set = new AnimatorSet();
        ObjectAnimator oa = ObjectAnimator.ofFloat(iv_ic, "translationX", new float[]{10f,20f,30f,40f,60f,80f});
        oa.setDuration(500);
        ObjectAnimator oa2 = ObjectAnimator.ofFloat(iv_ic, "translationY", new float[]{-10f,-20f,-30f,-40f,-60f,-80f});
        oa2.setDuration(500);
//        set.playTogether(oa,oa2);//一起播放
        set.playSequentially(oa,oa2);//先播放oa,在播放oa2。顺序播放的意思。
        set.start();
    }

    /**
     * 缩放
     */
    private void scalesAnim() {
        ObjectAnimator oa=ObjectAnimator.ofFloat(iv_ic,"scaleX",new float[]{1f,2f,3f});
        oa.setDuration(500);
        oa.setRepeatMode(ObjectAnimator.RESTART);
        oa.setRepeatCount(ObjectAnimator.INFINITE);
        oa.start();
    }

    /**
     * 透明
     */
    private void alpsAnim() {
//        ObjectAnimator oa=ObjectAnimator.ofFloat(iv_ic,"alpha",new float[]{0,0.2f,0.4f,0.6f,0.8f,1.0f});
//        oa.setDuration(500);
//        oa.setRepeatMode(ObjectAnimator.RESTART);
//        oa.setRepeatCount(ObjectAnimator.INFINITE);
//        oa.start();
        Animator animator=AnimatorInflater.loadAnimator(this,R.animator.alpha_demo);
        animator.setTarget(iv_ic);//设置要操作哪个控件。
        animator.start();
    }

    /**
     * 旋转
     */
    private void rototasAnim() {
        ObjectAnimator oa=ObjectAnimator.ofFloat(iv_ic,"rotationX",new float[]{0,90,180,270,360});
        oa.setDuration(500);
        oa.setRepeatMode(ObjectAnimator.RESTART);
        oa.setRepeatCount(ObjectAnimator.INFINITE);
        oa.start();
    }

    /**
     * 平移动画
     */
    private void transAnim() {
        //translationX:指的是x轴方向平移
        ObjectAnimator oa=ObjectAnimator.ofFloat(iv_ic,"translationX",new float[]{0,2,4,6,8,10,12,14,16,18,20,22,24,26,28});
        oa.setDuration(500);
        oa.setRepeatMode(ObjectAnimator.RESTART);
        oa.setRepeatCount(ObjectAnimator.INFINITE);
        oa.start();
    }
}
